<template>
  <div class="leave_add_container">
    <el-form :model="ruleForm" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker v-model="ruleForm.startTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker v-model="ruleForm.endTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="请假理由" prop="reason">
        <el-input type="text" v-model="ruleForm.reason" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleForm)">确定</el-button>
        <el-button @click="cancer">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addLeave } from '@/api/api'

export default {
  data () {
    return {
      userId: this.$store.state.userId,
      ruleForm: {
        startTime: '',
        endTime: '',
        reason: ''
      },
      departList: [],
      // date: new Date(),
      roleList: [],
      fileList: [],
      token: this.$store.state.token
    }
  },
  methods: {
    submitForm (formName) {
      // 获取当前日期
      this.timer = setInterval(function () {
        this.date = new Date().toLocaleString()
      })
      const params = {
        userId: this.userId,
        startTime: formName.startTime,
        endTime: formName.endTime,
        reason: formName.reason,
        submitTime: this.dateFormat()
      }
      addLeave(params).then(res => {
        // console.log(res.data)
        this.$router.back()
      })
    },
    cancer () {
      this.$router.back()
    },
    // 当前日期格式化
    dateFormat () {
      const date = new Date()
      const year = date.getFullYear()
      const month = date.getMonth() + 1 < 10
        ? '0' + (date.getMonth() + 1)
        : date.getMonth() + 1
      const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      const hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
      const minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
      const seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
      return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
    }
  }
}

</script>

<style lang="scss" scoped>
.leave_add_container {
  width: 50%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.el-form-item__label {
  margin-bottom: 10px;
}
</style>
